<?php

namespace Server\Controller;
use Think\Controller\HproseController;
class ArticleController extends HproseController {
    public function getArticleList($type,$page){
        header("Content-Type:text/html; charset = utf-8");

        if($type == 0){
            $count = M('article')->join('article_type on article_type.id = article.type')->where(array('article.del'=>0,'article.state'=>1))->order('article.createtime desc')->field('article.id,article_type.name as typename,article.createtime,article.title')->count();
            $result = M('article')->join('article_type on article_type.id = article.type')->where(array('article.del'=>0,'article.state'=>1))->order('article.createtime desc')->field('article.id,article_type.name as typename,article.createtime,article.title')->limit(10*($page-1),10)->select();
        }
        else{
            $count = M('article')->join('article_type on article_type.id = article.type')->where(array('article.del'=>0,'article.state'=>1,'article_type.id|article_type.parent_id'=>$type))->order('article.createtime desc')->field('article.id,article_type.name as typename,article.createtime,article.title')->count();
            $result = M('article')->join('article_type on article_type.id = article.type')->where(array('article.del'=>0,'article.state'=>1,'article_type.id|article_type.parent_id'=>$type))->order('article.createtime desc')->field('article.id,article_type.name as typename,article.createtime,article.title')->limit(10*($page-1),10)->select();
        }
        $res['success'] = 1;
        $res['result'] = $result;
        $res['count'] = $count;
        return json_encode($res);
    }

    public function getArticleDetail($aid){
        header("Content-Type:text/html; charset = utf-8");
        $user = getUserFromWid($_COOKIE["shu_ces_wechat_wid"]);
        if($user == ''){
            $result['success'] = 0;
            $result['info'] = 'wrong cookie!';
        }
        $read = count(M('read_history')->where(array('id'=>$aid,'user_id'=>$user['id'],'del'=>0))->select());
        if($read == 0){
            M('article')->where(array('id'=>$aid,'state'=>1,'del'=>0))->setInc('read_num',1);
            $data['article_id'] = $aid;
            $data['user_id'] = $user['id'];
            $data['createtime'] = date("Y-m-d H:i:s");
            M('read_history')->add($data);
        }
        $article = M('article')->join('article_type on article_type.id = article.type')->where(array('article.id'=>$aid,'article.state'=>1,'article.del'=>0))->field('article.title,article.author,article.createtime,article.content,article.read_num,article.good_num,article_type.name as typename')->find();
        if(count($article) == 0){
            $result['success'] = 0;
            $result['info'] = 'wrong article_id!';
        }
        else{
            $article['good'] = count(M('like_history')->where(array('type'=>1,'relative_id'=>$aid,'user_id'=>$user['id'],'del'=>0))->select());
            $comment = M('comment')->join('user on user.id = comment.user_id')->where(array('comment.article_id'=>$aid,'comment.state'=>1,'comment.del'=>0))->field('user.name,user.photo,comment.id,comment.createtime,comment.good_num,comment.content')->select();
            foreach ($comment as $k => $v) {
                $comment[$k]['good'] = count(M('like_history')->where(array('type'=>2,'relative_id'=>$v['id'],'user_id'=>$user['id'],'del'=>0))->select());
            }
            $result['article'] = $article;
            $result['comment'] = $comment;
            $result['comment_num'] = count($comment);
            $result['success'] = 1;
        }
        return json_encode($result);
    }

    public function comment($content,$aid){
        header("Content-Type:text/html; charset = utf-8");
        $user = getUserFromWid($_COOKIE["shu_ces_wechat_wid"]);
        if($user == ''){
            $result['success'] = 0;
            $result['info'] = 'wrong cookie!';
        }
        $t = M('comment')->where(array('user_id'=>$user['id']))->order('createtime desc')->getField('createtime');
        $dif = time()-strtotime($t);
        if($dif < 60){
            $result['success'] = 0;
            $result['info'] = '发言太频繁!';
        }
        else{
            $data['user_id'] = $user['id'];
            $data['article_id'] = $aid;
            $data['content'] = $content;
            $data['createtime'] = date("Y-m-d H:i:s");
            M('comment')->add($data);
            $result['success'] = 1;

        }
        return json_encode($result);
    }

    public function setGoodArticle($aid){
        header("Content-Type:text/html; charset = utf-8");
        $user = getUserFromWid($_COOKIE["shu_ces_wechat_wid"]);
        if($user == ''){
            $result['success'] = 0;
            $result['info'] = 'wrong cookie!';
        }
        $count = count( M('like_history')->where(array('type'=>1,'relative_id'=>$aid,'user_id'=>$user['id'],'del'=>0))->select());
        if($count > 0){
            $result['success'] = 0;
            $result['info'] = 'wrong article_id!';
        }
        else{
            $data['relative_id'] = $aid;
            $data['user_id'] = $user['id'];
            $data['type'] = 1;
            $data['createtime'] = date("Y-m-d H:i:s");
            M('like_history')->add($data);
            M('article')->where(array('id'=>$aid,'state'=>1,'del'=>0))->setInc('good_num',1);
            $result['good_num'] = M('article')->where(array('id'=>$aid,'state'=>1,'del'=>0))->getfield('good_num');
            $result['success'] = 1;
        }
        return json_encode($result);
    }
    public function setGoodComment($cid){
        header("Content-Type:text/html; charset = utf-8");
        $user = getUserFromWid($_COOKIE["shu_ces_wechat_wid"]);
        if($user == ''){
            $result['success'] = 0;
            $result['info'] = 'wrong cookie!';
        }
        $count = count( M('like_history')->where(array('type'=>2,'relative_id'=>$cid,'user_id'=>$user['id'],'del'=>0))->select());
        if($count > 0){
            $result['success'] = 0;
            $result['info'] = 'wrong comment_id!';
        }
        else{
            $data['relative_id'] = $cid;
            $data['user_id'] = $user['id'];
            $data['type'] = 2;
            $data['createtime'] = date("Y-m-d H:i:s");
            M('like_history')->add($data);
            M('comment')->where(array('id'=>$cid,'state'=>1,'del'=>0))->setInc('good_num',1);
            $result['good_num'] = M('comment')->where(array('id'=>$cid,'state'=>1,'del'=>0))->getfield('good_num');
            $result['id'] = $cid;
            $result['success'] = 1;
        }
        return json_encode($result);
    }
}
